import { Component, OnChanges,SimpleChanges,Input } from '@angular/core';
import { HeroService } from '../../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';
import { options } from '../../../../common/areaoption';

@Component({
  selector: 'app-setarea',
  templateUrl: './setarea.component.html',
  styleUrls: ['./setarea.component.css']
})
export class SetareaComponent implements OnChanges {

  @Input() id;
  isVisible=false;
  province;
  city;
  district;
  public nzOptions = options;
  public arealist: Array<any> = [];
  public values: Array<any> = ['请选择地区'];
  public selectArea:number;
  public onChanges(values: any): void {
    this.values=values;
    console.log(this.values);
    if(this.values[1]){
      this.selectArea=0;
    }
    if(this.values.length==0){
      this.selectArea=-1;
    }
  }
  constructor(private heroService: HeroService,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
  }

  ngOnChanges(changes: SimpleChanges) {
    this.getArealsit();
  }
  getArealsit(){
    if(this.id){
      let url=host+"/main/sysCityService/list";
      let model="type=0&userId="+this.id;
      this.heroService.post(url,model).then(result=>{
          this.arealist=result.data.pageBean.result;
          this.province=result.data.province;
          this.city=result.data.city;
          this.district=result.data.district;
      })
    }
  }
  delete(id){
    let url=host+"/main/sysCityService/del";
    let model='id='+id;
    this.heroService.post(url,model).then(result=>{
        console.log(result.code);
        if(result.code==0){
          this.message.success('删除成功！');
          this.getArealsit();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  addAera(){
    this.isVisible=true;
  }
  handleOk(){
    console.log(this.values);
    if(!this.values[2]){
      this.values[2]=-1;
    }
    if(!this.values[1]){
      this.values[1]=-1;
    }
    if(this.values[0] =="请选择地区"){
      this.selectArea=-1;
    }
    if(this.selectArea !==-1){
      let url=host+"/main/sysCityService/save";
      let model="type=0&userId="+this.id+"&provinceId="+this.values[0]+"&cityId="+this.values[1]+"&districtId="+this.values[2];
      this.heroService.post(url,model).then(result=>{
          if(result.code==0){
            this.message.success("添加管理区域成功");
            this.isVisible=false;
            this.getArealsit();
          }else{
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
      })
    }
  }
  handleCancel(){
    this.isVisible=false;
  }
}
